<?php
namespace app\center\logic;
use app\center\validate\MatterFlow as MatterFlowValidate;

class WaitMatterUserLogic extends BaseLogic {

    /**
     * 获取待办事项个人查询条件
     * @param $openid
     * @param $from_openid
     * @param $source
     * @param $start_time
     * @return array
     */
    public function get_search_wmu_maps($openid,$from_openid,$source,$start_time){
        $maps = [];
        $maps[] = ['deal_openid','=',$openid];
        $maps[] = ['status','=',2];

        if(!empty($from_openid)){
            $maps[] = ['from_openid','=',$from_openid];
        }

        if($source != -1){
            $maps[] = ['source','=',$source];
        }

        if(!empty($start_time)){
            $maps[] = ['c_time','like',$start_time . '%'];
        }
        return $maps;
    }

    /**
     * 检查处理待办事项个人规则（巡店）
     * @param $openid
     * @param $params
     * @return mixed
     */
    public function check_deal_wmu_cp_rule($openid,$params){
        $validate = new MatterFlowValidate();
        $r = $validate->scene('add')->check($params);
        if(!$r){
            return $this->get_response(0,$validate->getError());
        }

        $maps = [];
        $maps[] = ['id','=',$params['matterid']];
        $maps[] = ['deal_openid','=',$openid];
        $maps[] = ['status','=',2];
        $m_detail = model('matter')->get_detail($maps,'id,sourceid,deal_openid');
        if(empty($m_detail)){
            return $this->get_response(0,'该待办事项不存在');
        }

        if($params['action'] == 103){
            if(empty($params['out_time'])){
                return $this->get_response(0,'处理时间必须填写');
            }
        }

        if($params['action'] == 104){
            if(empty($params['deal_openid'])){
                return $this->get_response(0,'移交对象必须填写');
            }
        }

        $data['m_detail'] = $m_detail;
        return $this->get_response(1,'OK',$data);
    }

    /**
     * 检查处理待办事项个人规则（协同）
     * @param $openid
     * @param $params
     * @return mixed
     */
    public function check_deal_wmu_xt_rule($openid,$params){
        $validate = new MatterFlowValidate();
        $r = $validate->scene('add')->check($params);
        if(!$r){
            return $this->get_response(0,$validate->getError());
        }

        $maps = [];
        $maps[] = ['id','=',$params['matterid']];
        $maps[] = ['deal_openid','=',$openid];
        $maps[] = ['status','=',2];
        $m_detail = model('matter')->get_detail($maps,'id,sourceid,deal_openid');
        if(empty($m_detail)){
            return $this->get_response(0,'该待办事项不存在');
        }

        if($params['action'] == 103){
            if(empty($params['out_time'])){
                return $this->get_response(0,'处理时间必须填写');
            }
        }

        if($params['action'] == 104){
            if(empty($params['deal_openid'])){
                return $this->get_response(0,'移交对象必须填写');
            }
        }

        $data['m_detail'] = $m_detail;
        return $this->get_response(1,'OK',$data);
    }

    /**
     * 更新事项为已读
     * @param $matterid
     * @return mixed
     */
    public function save_matter_read($matterid){
        $maps = [];
        $maps[] = ['id','=',$matterid];

        $m_data = [];
        $m_data['is_read'] = 1;
        $r = model('matter')->force()->save($m_data,$maps);
        if($r){
            return $this->get_response(1,'OK');
        }

        return $this->get_response(1,'OK');
    }
}